﻿using VirtualTournaments.DataAccess.SqlServer.DataProxies;

namespace VirtualTournaments.DataAccess.SqlServer.DataMappers
{
	internal sealed class MatchesDataMapper : AbstractDataMapper<SqlServer.Match, Business.Tournaments.Match, DataProxies.MatchDataProxy>
	{
		#region Singleton implementation

		private static MatchesDataMapper _instance = null;
		private static object _instanceLocker = new object();

		private MatchesDataMapper() { }

		public static MatchesDataMapper Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_instanceLocker)
					{
						if (_instance == null)
						{
							_instance = new MatchesDataMapper();
						}
					}
				}
				return _instance;
			}
		}

		#endregion

		internal override VirtualTournamentsDataContext GetDataContext()
		{
			return VirtualTournamentsDataContext.Instance;
		}

		internal override System.Data.Linq.Table<Match> GetDataTable()
		{
			return GetDataContext().Matches;
		}

		internal override Business.Tournaments.Match DataObject2BusinessObject(SqlServer.Match dataObject)
		{
			return new MatchDataProxy(dataObject);
		}

		internal override void BusinessObject2DataObject(Business.Tournaments.Match bizMatch, SqlServer.Match dataMatch)
		{
			if (bizMatch.Local != null)
			{
				SqlServer.Team dataTeam = TeamsDataMapper.Instance.GetOneAsDataObject(team => team.Name == bizMatch.Local.Name);
				if (dataTeam != null)
					dataMatch.LocalTeam = dataTeam;
				else
					dataMatch.LocalTeam = TeamsDataMapper.Instance.Save(bizMatch.Local);
			}
			if (bizMatch.Visitor != null)
			{
				SqlServer.Team dataTeam = TeamsDataMapper.Instance.GetOneAsDataObject(team => team.Name == bizMatch.Visitor.Name);
				if (dataTeam != null)
					dataMatch.VisitorTeam = dataTeam;
				else
					dataMatch.VisitorTeam = TeamsDataMapper.Instance.Save(bizMatch.Visitor);
			}
			dataMatch.IsPlayed = bizMatch.IsPlayed;
			if (bizMatch.Result != null)
			{
				dataMatch.LocalGoals = bizMatch.Result.LocalGoals;
				dataMatch.VisitorGoals = bizMatch.Result.VisitorGoals;
			}
		}

		internal override void DataProxy2DataObject(MatchDataProxy proxyMatch, SqlServer.Match dataMatch)
		{
			//TODO: untested
			if (proxyMatch.LocalHasBeenRead)
			{
				SqlServer.Team dataTeam = TeamsDataMapper.Instance.GetOneAsDataObject(team => team.Name == proxyMatch.Local.Name);
				if (dataTeam != null)
					dataMatch.LocalTeam = dataTeam;
				else
					dataMatch.LocalTeam = TeamsDataMapper.Instance.Save(proxyMatch.Local);
			}
			if (proxyMatch.VisitorHasBeenRead)
			{
				SqlServer.Team dataTeam = TeamsDataMapper.Instance.GetOneAsDataObject(team => team.Name == proxyMatch.Visitor.Name);
				if (dataTeam != null)
					dataMatch.VisitorTeam = dataTeam;
				else
					dataMatch.VisitorTeam = TeamsDataMapper.Instance.Save(proxyMatch.Visitor);
			}
			dataMatch.IsPlayed = proxyMatch.IsPlayed;
			if (proxyMatch.Result != null)
			{
				dataMatch.LocalGoals = proxyMatch.Result.LocalGoals;
				dataMatch.VisitorGoals = proxyMatch.Result.VisitorGoals;
			}
		}
	}
}
